package com.sskj.mine.ui.activity;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.MineVerificationMenuPresenter;
import com.sskj.mine.util.HideUtil;
import com.sskj.mine.util.TipMineUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.MINE_VERIFICATION_MENU)//我的主页
public class MineVerificationMenuActivity extends BaseActivity<MineVerificationMenuPresenter> {

    @BindView(R2.id.ll_junior)
    LinearLayout llJunior;
    @BindView(R2.id.ll_high)
    LinearLayout llHigh;
    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.junior_tv)
    TextView juniorTv;
    @BindView(R2.id.high_tv)
    TextView highTv;
    @BindView(R2.id.verification_low_img)
    ImageView verificationLowImg;
    @BindView(R2.id.verification_high_img)
    ImageView verificationHighImg;
    @BindView(R2.id.tv_low)
    TextView tvLow;
    private UserData userData;
    private MaterialDialog tip;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_verification_menu;
    }

    @Override
    public MineVerificationMenuPresenter getPresenter() {
        return new MineVerificationMenuPresenter();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            updateUI();
            updateUiJunior();
        });
        ClickUtil.click(llJunior, () -> {
            checkIdent();
        });
        ClickUtil.click(llHigh, () -> {
            if (2 == userData.getBasicStatus()) {
                ARouter.getInstance().build(RConfig.MINE_VERFICATION_HIGH).navigation();
            } else {
                ToastUtil.showShort("尚未初级实名认证");
            }
        });
    }

    public void updateUI() {  // 高级认证状台
        switch (userData.getStatus()) {       // 实名认证
            case 1:
                highTv.setText(App.INSTANCE.getString(R.string.mineSecurityCenterActivity10));
                verificationHighImg.setVisibility(View.GONE);
                break;
            case 2:
                highTv.setText(App.INSTANCE.getString(R.string.mineSecurityCenterActivity26));
                verificationHighImg.setVisibility(View.GONE);
                break;
            case 3:
                highTv.setText(App.INSTANCE.getString(R.string.mineSecurityCenterActivity27));
                verificationHighImg.setVisibility(View.VISIBLE);
                break;
            case 4:
                highTv.setText(App.INSTANCE.getString(R.string.mineSecurityCenterActivity28));
                verificationHighImg.setVisibility(View.GONE);
                break;
        }
    }

    public void updateUiJunior() {  // 初级认证状台
        switch (userData.getBasicStatus()) {
            case 1:
                juniorTv.setText(App.INSTANCE.getString(R.string.mineSecurityCenterActivity10));
                verificationLowImg.setVisibility(View.GONE);
                break;
            case 2:
                juniorTv.setText(App.INSTANCE.getString(R.string.mineSecurityCenterActivity27));
                tvLow.setText(userData.getUsername() + " | " + HideUtil.getIDCardHide(userData.getIdCardNo()));
                verificationLowImg.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        userViewModel.update();
    }

    /**
     * 判断用户实名认证
     */
    public void checkIdent() {
        switch (userData.getStatus()) {
            case 3:
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSecurityCenterActivity13));
                break;
            case 2:
                showApplyTip(App.INSTANCE.getString(R.string.mineSecurityCenterActivity14), false);
                break;
            default:
                ARouter.getInstance().build(RConfig.MINE_VERFICATION_JUNIOR).navigation();
                break;
        }
    }

    /**
     * 判断用户实名认证弹窗
     */
    private void showApplyTip(String msg, boolean status) {
        if (tip == null) {
            tip = TipMineUtil.getTip(this, App.INSTANCE.getString(R.string.mineSecurityCenterActivity15),
                    msg,
                    App.INSTANCE.getString(R.string.mineSecurityCenterActivity17),
                    () -> {
                        tip.dismiss();
                        if (status) {
                            ARouter.getInstance().build(RConfig.MINE_CERTIFICATION).navigation();
                        }
                    });
        }
        tip.show();
    }


}
